 var table ,form ,laypage ,layer ,element ,laydate ,upload;//时间组件
var check=true;
$(function(){ 
	var hid=getQueryString("id");
	var saveurl="/loop/saveLoop.do";
	layui.use(['table','form','laypage','layer', 'element', 'laydate','upload'], function(){
		 table = layui.table;
		 form=layui.form;
		 laypage = layui.laypage ;//分页
		 layer = layui.layer ;//弹层
		 element = layui.element; //元素操作
		 laydate = layui.laydate; //时间组件 
		 upload = layui.upload;
		 dictPublicfun();
		 form.render();
		 setTimeout(function () {
		        if(hid){//编辑
		      	   $("#Title").html("回路信息编辑");    	   
		      	   saveurl="/loop/editLoop.do";
		      	   var questData = {"id": hid};
		       	  	$.ajax({
		   	           url: "/loop/getOneLoop.do",               //请求地址
		   	           type: "POST",              //请求类型
		   	           data:formatParam(questData),              //请求数据
		   	           timeout: 200000,                //请求超时时间(毫秒)
		   	           headers: setHeader(),
		   	           success:function(res){         //请求成功
		   	        	   if(res.status == 200)
		   	  				{
		   	        		   	var num=res.resultData;
		   	        		    $("#id").val(hid);  	        		    
		   	    				$("#name").val(num.name); 	    				  	    				
		   	    				$("#stationName").val(num.stationName); 
		  	        		    $("#station").val(num.station);
		  	        		    $("#stationId").val(num.station);
		  	        		    $("#ct").val(num.ct);
		  	        		    $("#type").val(num.type);
		  	        		    $("#current").val(num.current);
		  	        		    $("#telemetering").val(num.telemetering);
		  	        		    $("#telesignalling").val(num.telesignalling);
		 	 	        		$("#supplyCircuit").val(num.supplyCircuit);
		 	 	        		$("#switchCabinetName").val(num.switchCabinetName); 
		  	        		    $("#switchCabinet").val(num.switchCabinet);
		 	 	        		$("#collectorAddress").val(num.collectorAddress);
		 	 	        		$("#serialNumber").val(num.serialNumber);
		 	 	        		$("#itemType").val(num.itemType);
		 	 	        		$("#voltageLevel").val(num.voltageLevel);
		 	 	        		$("#detailedInformation").val(num.detailedInformation);
		 	 	        		form.render();
		 	 	        		if(num.detailedInformation==2){//详细
		 	 	         		    $("#Informationform").show()
		 	 	         		    $("#demandLimit").val(num.demandLimit);
		 	 	        		    $("#generalCurrentLimit").val(num.generalCurrentLimit);
		 	 	        		    $("#currentSevereLimit").val(num.currentSevereLimit);
		 	 	        		    $("#generalUpperLimit").val(num.generalUpperLimit);
		 	 	        		    $("#criticalUpperLimit").val(num.criticalUpperLimit);
		 		 	        		$("#commonLowerLimit").val(num.commonLowerLimit);
		 		 	        		$("#criticalLowerLimit").val(num.criticalLowerLimit);
		 		 	        		$("#ratedCapacity").val(num.ratedCapacity);
		 		 	        		$("input[name='importantEquipment'][value='0']").attr("checked", num.importantEquipment == 0 ? true : false);
		 		 	                $("input[name='importantEquipment'][value='1']").attr("checked", num.importantEquipment == 1 ? true : false);
		 		 	                
		 		 	                $("input[name='alarm'][value='0']").attr("checked", num.alarm == 0 ? true : false);
		 		 	                $("input[name='alarm'][value='1']").attr("checked", num.alarm == 1 ? true : false);
		 		 	                
		 		 	                $("input[name='send_wechat'][value='0']").attr("checked", num.sendWechat == 0 ? true : false);
		 		 	                $("input[name='send_wechat'][value='1']").attr("checked", num.sendWechat == 1 ? true : false);
		 		 	                
		 		 	                $("input[name='twoTableMethod'][value='0']").attr("checked", num.twoTableMethod == 0 ? true : false);
		 		 	                $("input[name='twoTableMethod'][value='1']").attr("checked", num.twoTableMethod == 1 ? true : false);
		 		 	               form.render();
		 		 	         	}else{
		 		 	         		$("#Informationform").hide()
		 		 	         	};	 	        		
		 	    				$("#remarks").val(num.remarks);
		   	    				form.render();
		   	  				}else{          	  				    
		   	  					layer.msg(res.resultMsg, {icon: res.icon});
		   	  				}
		   	  				      
		                 }
		              })
		         }
		 }, 1000);
		 //变电站选择变化
		
		 $('#station').on('input propertychange', function() {
			 $("#switchCabinetName").val(""); 
   		     $("#switchCabinet").val("");
         }) 
        //监听详细信息
        form.on('select(finformation)', function(data){
        	  if(data.value==2){//详细
        		  $("#Informationform").show()
        	  }else{
        		  $("#Informationform").hide()
        	  };
        });         
    	 //表单验证
        form.verify({	  
        	fname: function(value, item)
	          { 
	  			  if(validate.isBlank(value))
	  			  {
	  				 return "请填写名称";
	  			  }
	  			 if(!validate.isLength(value,1,50))
  		      {
  		         return '名称长度不能超过50个字符';
  		      }
	  	  },
	     fct: function(value, item)
         { 
 			  if(validate.isBlank(value))
 			  {
 				 return "请选择CT变比";
 			  }
 	      },
	     fstation: function(value, item)
	        {  
			  if(validate.isBlank(value))
			  {
				 return "请选择所属变电站";
			  }
	     },
	     ftype: function(value, item)
	        {  
			  if(validate.isBlank(value))
			  {
				 return "请选择类型";
			  }
	     },
	     fitemType: function(value, item)
	        {  
			  if(validate.isBlank(value))
			  {
				 return "请选择分项类型";
			  }
	     },
	     finformation: function(value, item)
	        {  
			  if(validate.isBlank(value))
			  {
				 return "请选择详细信息";
			  }
	     },
         fremarks: function(value, item)
	          {   //备注长度验证
	  			 if(!validate.isLength(value,0,200))
  		      {
  		         return '备注长度不能超过200个字符';
  		      }
	  	   }
		  	          
       })
      form.on('submit(formDemo)', function(data){  
			this.blur();//防止点回车，按钮重复点击
			var loadIndex = layer.load(1);//显示加载层
			//判断时间为空值为null
			var requestData= formToJsonObj("form_edit", []);
			$.ajax({
				url:saveurl,
				type:'POST',
				async:false,
				data:formatParam(requestData),
				timeout: 200000,
				headers: setHeader(),
				success:function(res){         //请求成功
		        	   if(res.status == 200) {  
		        		   layer.msg(res.resultMsg, {icon: res.icon},function(){
		   					     var index = parent.layer.getFrameIndex(window.name);
		   					      parent.layui.table.reload("tabledata")//
		   					      parent.layer.close(index);//关闭弹出的子页面窗口
		   					}); 					
		        	    }else if(res.status == 666){
		  					TIMEOUT(); 
		  		        }else{
		  					layer.msg(res.resultMsg, {icon: res.icon});		  					
		  				}  
		        	   layer.close(loadIndex);//关闭加载层
	              }		
			})
		  return false; //阻止表单跳转。
	  })
   }) 	
})
function aa(e){alert("inputting!!");}
function CheckInputFloat(ele){
	var str =$(ele).val();
	$(ele).val($(ele).val().replace(/[^\d.]/g,"")); //清除"数字"和".“以外的字符
	$(ele).val($(ele).val().replace(/^0/g, ""));//(/^./g,”"); //验证第一个字符是数字
	$(ele).val($(ele).val().replace(/\.{2,}/g,".")); //只保留第一个, 清除多余的
	$(ele).val($(ele).val().replace(".","$#$").replace(/\./g,"").replace("$#$","."));
	$(ele).val($(ele).val().replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')); //只能输入两个小数
}